<script setup>
import { ref, onMounted } from 'vue'
import { useAuthStore } from '@/stores/auth'

const authStore = useAuthStore()
const tokenValue = ref('')
const storeInfo = ref('')

// 模拟解析JWT的函数
const parseJwt = (token) => {
  try {
    const base64Url = token.split('.')[1]
    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/')
    const jsonPayload = decodeURIComponent(atob(base64).split('').map((c) => {
      return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
    }).join(''))
    return JSON.parse(jsonPayload)
  } catch (e) {
    console.error('解析JWT失败:', e)
    return { error: '无效的JWT格式' }
  }
}

// 加载状态
onMounted(() => {
  updateStoreInfo()
})

// 更新store信息
const updateStoreInfo = () => {
  // 获取store当前状态
  tokenValue.value = authStore.token
  
  const info = {
    isLoggedIn: authStore.isLoggedIn,
    username: authStore.username,
    token: authStore.token ? '已存储' : '未存储',
    tokenDecoded: authStore.token ? parseJwt(authStore.token) : null
  }
  
  storeInfo.value = JSON.stringify(info, null, 2)
}

// 清除token
const clearToken = () => {
  authStore.logout()
  updateStoreInfo()
}
</script>

<template>
  <div class="auth-store-demo">
    <h2>Pinia Auth Store 演示</h2>
    
    <div class="status-section">
      <h3>当前登录状态</h3>
      <div class="status-badge" :class="{ 'logged-in': authStore.isLoggedIn }">
        {{ authStore.isLoggedIn ? '已登录' : '未登录' }}
      </div>
    </div>
    
    <div class="store-info">
      <h3>Store 信息</h3>
      <pre>{{ storeInfo }}</pre>
    </div>
    
    <div class="actions">
      <button class="refresh-btn" @click="updateStoreInfo">刷新状态</button>
      <button v-if="authStore.isLoggedIn" class="clear-btn" @click="clearToken">清除登录信息</button>
      <button v-else class="login-btn" @click="$router.push('/login')">去登录</button>
    </div>
  </div>
</template>

<style scoped>
.auth-store-demo {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

h2 {
  color: #333;
  margin-bottom: 20px;
  text-align: center;
}

h3 {
  color: #555;
  margin-bottom: 10px;
}

.status-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.status-badge {
  background-color: #f44336;
  color: white;
  padding: 5px 10px;
  border-radius: 20px;
  font-weight: bold;
}

.status-badge.logged-in {
  background-color: #4caf50;
}

.store-info {
  margin-bottom: 20px;
}

pre {
  background-color: #f0f0f0;
  padding: 15px;
  border-radius: 4px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s ease;
}

.refresh-btn {
  background-color: #2196f3;
  color: white;
}

.refresh-btn:hover {
  background-color: #0d8bf2;
}

.clear-btn {
  background-color: #f44336;
  color: white;
}

.clear-btn:hover {
  background-color: #e53935;
}

.login-btn {
  background-color: #4caf50;
  color: white;
}

.login-btn:hover {
  background-color: #43a047;
}
</style> 